import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { createServer } from 'http';
import { join } from 'path';
import { transformDate } from './utils/dateUtils';
import userRouter from './router/userRouter';
const app = express();
const server = createServer(app);
const port = process.env.PORT || 4000;
app.use(userRouter);

app.use(bodyParser.json());
app.use(cors());
app.use(express.static(join(__dirname, '../public')));

app.get('/', (req, res) => {
  console.log(transformDate(new Date()));
  // 发送一个页面
  res.sendFile(join(__dirname, '../public/index.html'));
});

app.get('/user', (req, res) => {
  // 发送一个页面
  console.log(transformDate(new Date()));
  res.sendFile(join(__dirname, '../public/data.json'));
});
// app.listen(port, () => {
//   console.log(`Server started on port ${port}`)
// })

server.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});
